#include "dialog.ch"

//----------------------------------------------------------------------------//

CLASS CheckBtn FROM Visual

   DATA  cText
   DATA  lChecked
   DATA  cClrHotKey

   METHOD New        
   METHOD Display    
   METHOD SetFocus   
   METHOD KeyPressed 
   METHOD Click      
   METHOD DblClick   
   METHOD cGetHotKey 
   METHOD SetText    

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nRow, nCol, cText, nId, cMessage ) CLASS CheckBtn

   DEFAULT nRow = 0, nCol = 0, cText = "&CheckButton", nId = 0, cMessage = ""

   ::Parent:New()

   ::nTop       = nRow
   ::nLeft      = nCol
   ::nBottom    = nRow
   ::nRight     = nCol + 5 + nLabelLen( cText ) - 1
   ::nId        = nId
   ::cText      = cText
   ::cMessage   = cMessage
   ::lChecked   = .f.
   ::cClrHotKey = "GR+" + SubStr( ::cClrNormal, At( "/", ::cClrNormal ) )

return Self

//----------------------------------------------------------------------------//

METHOD Display() CLASS CheckBtn

   local nMCrsOld := SetMCursor()

   SetMCursor( .f. )
   SayHot( ::nAbsTop(), ::nAbsLeft(),;
           " [" + If( ::lChecked, "X", " " ) + "] " + ::cText,;
           If( ! ::lActive, ::cClrInactive,;
               If( ::lFocused, ::cClrFocus, ::cClrNormal ) ),;
               If( ::lFocused, SubStr( ::cClrHotKey, 1, At( "/", ::cClrHotKey ) - 1 ) + ;
                   SubStr( ::cClrFocus, At( "/", ::cClrFocus ) ), ::cClrHotKey ) )
   SetMCursor( nMCrsOld )

return nil

//----------------------------------------------------------------------------//

METHOD SetFocus( lOnOff, cClrFocus ) CLASS CheckBtn

   ::Parent:SetFocus( lOnOff, cClrFocus )
   ::Display()

return nil

//----------------------------------------------------------------------------//

METHOD KeyPressed( nKey ) CLASS CheckBtn

   ::lChecked = ! ::lChecked
   if ::oParent != nil
      ::oParent:CbtChanged( Self )
   endif
   ::Display()

return nil

//----------------------------------------------------------------------------//

METHOD Click( nMRow, nMCol ) CLASS CheckBtn

   ::lChecked = ! ::lChecked
   if ::oParent != nil
      ::oParent:CbtChanged( Self )
   endif
   ::Display()
   do while lMPressed()
      MUpdate()
   enddo

return nil

//----------------------------------------------------------------------------//

METHOD DblClick( nMRow, nMCol ) CLASS CheckBtn

   ::Click( nMRow, nMCol )

return nil

//----------------------------------------------------------------------------//

METHOD cGetHotKey() CLASS CheckBtn

   local nAt := At( "&", ::cText )

return If( nAt != 0, SubStr( ::cText, nAt + 1, 1 ), "" )

//----------------------------------------------------------------------------//

METHOD SetText( cText ) CLASS CheckBtn

   ::cText  = cText
   ::nRight = ::nLeft + 5 + nLabelLen( cText ) - 1

return nil

//----------------------------------------------------------------------------//